package csc.fresher.finalgroupfour.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import csc.fresher.finalgroupfour.dao.TransactionDAO;
import csc.fresher.finalgroupfour.domain.Account;
import csc.fresher.finalgroupfour.domain.Role;
import csc.fresher.finalgroupfour.domain.State;
import csc.fresher.finalgroupfour.domain.Transaction;
import csc.fresher.finalgroupfour.domain.User;

@Service
public class TransactionService {
	@Autowired
	private TransactionDAO transactionDAO;
	
	@Autowired
	private StateService stateService;

	@Autowired
	private AccountService accountService;
	
	public void addTransaction(Transaction transaction) {
		transactionDAO.addTransaction(transaction);
	}

	public List<Transaction> listTransactions() {
		return transactionDAO.listTransactions();
	}
	
	public List<Transaction> listTransactions(User user) {
		Role role = user.getRole();
		String roleName = role.getRole();
		List<Transaction> listTransaction = null;
		if (roleName.equals("ROLE_ADMIN")) {
			listTransaction = listTransactions();
		} else if (roleName.equals("ROLE_USER")) {
			listTransaction = findTransactionsByUser(user);
		}
		return listTransaction;
	}

	public void updateTransaction(Transaction transaction) {
		transactionDAO.updateTransaction(transaction);
	}

	public void deleteTransaction(Transaction transaction) {
		transactionDAO.deleteTransaction(transaction);
	}
	
	public List<Transaction> findTransactionsByUser(User user){
		return transactionDAO.findTransactionsByUser(user);
	}

	public void approveTransaction(Transaction transaction,
			String transactionState) {
		State state = transaction.getTransactionState();
		Account sourceAccount = transaction.getSourceAccount();
		Account targetAccount = transaction.getTargetAccount();
		long srcBalance = Long.parseLong(sourceAccount.getBalance());
		long desBalance = Long.parseLong(targetAccount.getBalance());
		long amount = transaction.getAmount();
		if (state.getState().equals("pending")) {
			if (transactionState.equals("approved")) {
				state = stateService.findState("approved");
				targetAccount.setBalance(String.valueOf(desBalance + amount));
				accountService.updateAccount(targetAccount);
			} else if (transactionState.equals("rejected")) {
				state = stateService.findState("rejected");
				sourceAccount.setBalance(String.valueOf(srcBalance + amount));
				accountService.updateAccount(sourceAccount);
			}

			transaction.setTransactionState(state);
			transaction.setEndDate(new Date());
			updateTransaction(transaction);
		}
	}

	public boolean makeNewTransaction(Account srcAcc, Account desAcc, long amount) {
		long srcBalance = Long.parseLong(srcAcc.getBalance());
		Date now = new Date();
		if (srcBalance < amount) {
			return false;
		} else {
			srcAcc.setBalance(String.valueOf(srcBalance - amount));
			accountService.updateAccount(srcAcc);
			State state = stateService.findState("pending");
			Transaction transaction = new Transaction(srcAcc, desAcc,
					amount, now, null, state);
			addTransaction(transaction);
			return true;
		}
	}

	
}
